package com.junhong.auth.service;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.ejb.embeddable.EJBContainer;
import javax.inject.Inject;
import javax.naming.Context;

import com.junhong.auth.entity.User;
import com.junhong.forum.dao.UserDAO;
import com.junhong.util.EncryptionUtil;

/**
 * Session Bean implementation class UserEjb
 */
@Stateless
@LocalBean
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserEjb {

	@Inject
	private UserDAO	userDAO;

	/**
	 * Default constructor.
	 */
	public UserEjb() {
	}

	/*
	 * @PostConstruct public void init() { ((EntityDAOImpl) userDAO).setEm(em);
	 * }
	 */

	/**
	 * @return
	 */
	public List<User> getUsers() {
		return userDAO.findAll(User.class);
	}

	/**
	 * @param userId
	 * @return
	 */
	public User getUserById(int userId) {
		return userDAO.findById(User.class, userId);
	}

	/**
	 * @param user
	 */
	public void createUser(User user) {
		userDAO.create(user);
	}

	/**
	 * @param user
	 */
	public void updateUser(User user) {
		userDAO.update(user);
	}

	/**
	 * @param user
	 */
	public void deleteUser(User user) {
		userDAO.delete(user);
	}

	/**
	 * @param userId
	 * @return
	 */
	public boolean isExist(String userId) {
		return getUserByUserId(userId) != null;
	}

	/**
	 * @param userId
	 * @return
	 */
	public User getUserByUserId(String userId) {
		// return
		// userDAO.findByHQLSingleResult("select user from User user where user.userId='"
		// + userId + "'", User.class);

		String query = "select user from User user where user.userId=?1";
		Object[] parms = { userId };
		return userDAO.findByHQLSingleResult(query, User.class, parms);
	}

	public void refreshUser(User user) {
		userDAO.refresh(user);

	}

	/**
	 * @param user
	 * @return the matching user if found based on userid otherwise, return
	 *         null;
	 */
	public User getMatchingUser(User user) {

		String md5Pwd = EncryptionUtil.md5(user.getPassword());
		// get the first user
		User userTemp = getUserByUserId(user.getUserId());

		if (null != userTemp) {
			if (userTemp.getPassword().equals(md5Pwd)) {
				return userTemp;
			}
		}
		return null;
	}

	/**
	 * get all the userIds based on the prefix.
	 * 
	 * @param prefix
	 * @return
	 */
	public List<String> getUserIds(String prefix) {
		return userDAO.getUserIds(prefix);
	}

	public User getUserWithEagerStickyThread(int id) {
		User user = userDAO.findById(User.class, id);
		user.getStickyThreads().size();

		return user;
	}

	/**
	 * update the number of Reply for the given User
	 * 
	 * @param user
	 * @param delta
	 */
	public void updateNumOfReplies(User user, int delta) {
		User currUser = this.getUserById(user.getId());
		currUser.setNumOfReplies(currUser.getNumOfReplies() + delta);
		this.updateUser(currUser);
	}

	public void updateNumOfPosts(User user, int delta) {
		User currUser = this.getUserById(user.getId());
		currUser.setNumOfPosts(currUser.getNumOfPosts() + delta);
		this.updateUser(currUser);
	}

	public static void main(String[] args) {
		User user = new User();
		user.setFirstName("andrew");
		user.setLastName("zhang");
		EJBContainer ec = null;
		try {
			ec = EJBContainer.createEJBContainer();
			Context ctx = ec.getContext();
			UserEjb userEjb = (UserEjb) ctx.lookup("java:global/forum/UserEjb");
			userEjb.createUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ec.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

}
